package com.ruoyi.pingjiao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pingjiao.domain.*;
import com.ruoyi.pingjiao.mapper.CourseclassMapper;
import com.ruoyi.pingjiao.mapper.EvaluationMapper;
import com.ruoyi.pingjiao.mapper.EvaluationPaperMapper;
import com.ruoyi.pingjiao.service.IEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pingjiao.mapper.EvalutionActivityMapper;
import com.ruoyi.pingjiao.service.IEvalutionActivityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评教活动管理Service业务层处理
 *
 * @author 胡浩鹏
 * @date 2022-10-22
 */
@Service
public class EvalutionActivityServiceImpl implements IEvalutionActivityService
{
    @Autowired
    private EvalutionActivityMapper evalutionActivityMapper;

    @Autowired
    private CourseclassMapper courseclassMapper;

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private IEvaluationService evaluationService;
    @Autowired
    private EvaluationPaperMapper evaluationPaperMapper;
    /**
     * 查询评教活动管理
     *
     * @param evalutionActivityId 评教活动管理主键
     * @return 评教活动管理
     */
    @Override
    public EvalutionActivity selectEvalutionActivityByEvalutionActivityId(Long evalutionActivityId)
    {
        return evalutionActivityMapper.selectEvalutionActivityByEvalutionActivityId(evalutionActivityId);
    }
    @Override
    public List<EvalutionActivity> selectEvalutionActivityByStudentId(Long studentId,String status){
        Map<String ,Object > map = new HashMap<String ,Object>();
        map.put("studentId",studentId);
        map.put("status",status);
        return  evalutionActivityMapper.selectEvalutionActivityByStudentId(map);
    }

    /**
     * 查询评教活动管理列表
     *
     * @param evalutionActivity 评教活动管理
     * @return 评教活动管理
     */
    @Override
    public List<EvalutionActivity> selectEvalutionActivityList(EvalutionActivity evalutionActivity)
    {
        return evalutionActivityMapper.selectEvalutionActivityList(evalutionActivity);
    }
    @Override
    public List<EvalutionActivity> selectEvalutionActivityList2(EvalutionActivity evalutionActivity)
    {
        return evalutionActivityMapper.selectEvalutionActivityList2(evalutionActivity);
    }





    /**
     * 修改评教活动管理
     *
     * @param evalutionActivity 评教活动管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateEvalutionActivity(EvalutionActivity evalutionActivity)
    {
        evalutionActivity.setUpdateTime(DateUtils.getNowDate());
        int i = evalutionActivityMapper.updateEvalutionActivity(evalutionActivity);
        //System.out.println(evalutionActivity);
        //如果更改状态为进行中执行
        /*
        if (evalutionActivity.getStatus().equals("1")){
            //预备数组
            List<Evaluation> evaluationList = new ArrayList<>();
            //预备对象
            Evaluation evaluation = new Evaluation();
            Courseclass courseclass = new Courseclass();
            courseclass.setTerm(evalutionActivity.getTerm());
            //获取当前学期的上课班级列表
            List<Courseclass> courseclasses = courseclassMapper.selectCourseclassList(courseclass);
            for (Courseclass cc : courseclasses){
                //设置评教活动id
                evaluation.setEvaluationActivityId(evalutionActivity.getEvalutionActivityId());
                //设置上课班级id
                evaluation.setCourseclassId(cc.getCourseclassId());
                //System.out.println(cc);
                //System.out.println(cc.getCourse());
                //System.out.println(cc.getTeacherList());
                //System.out.println(cc.getStudentList());
                //设置评卷类型
                if(cc.getCourse().getCourseType()=="4"){
                    evaluation.setEvaluationPaperId((long) 2);
                }
                else {
                    evaluation.setEvaluationPaperId((long) 1);
                }
                //设置学生id
                for (Student s : cc.getStudentList()){
                    evaluation.setStudentId(s.getStudentId());
                    //设置教师id
                    for (Teacher t : cc.getTeacherList()){

                        evaluation.setTeacherId(t.getTeacherId());
                        //设置创建时间
                        evaluation.setCreateTime(DateUtils.getNowDate());
                        evaluationList.add(evaluation);

                        evaluationMapper.insertEvaluation(evaluation);
                    }
                }

            }

            for (Evaluation e : evaluationList){
                System.out.println(e);
            }
            System.out.println(evaluationList.size());
            //evaluationMapper.insertEvaluations(evaluationList);
        }
         */
        return i;
    }

    /**
     * 修改活动状态并统计所有参评教师的评价成绩
     * @return
     */
    @Transactional
    @Override
    public  boolean calculateEvaluationScoresOfTeacher(){
        //1. 更新活动的状态为“3”（等待计算）
        updateEvalutionActivityStatusTo3();
        //2.计算教师的成绩
        statsEvaluationScoresOfTeacher();
        //3. 检查是否计算完成,小于5人就认为完成了
        if(statNumberOfNoEvaluatedTeacher()<5)
        {
            //4. 更新活动的状态为“4”（评价结束）
            updateEvalutionActivityStatusTo4();
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int updateEvalutionActivityStatusTo3() {
        return evalutionActivityMapper.updateEvalutionActivityStatusTo3();
    }
    /**
     * 设置activity status为4的（评价结束）
     * @return
     */
    @Override
    public int updateEvalutionActivityStatusTo4() {
        return evalutionActivityMapper.updateEvalutionActivityStatusTo4();
    }

    /**
     * 统计所有参评教师的评价成绩
     * @return
     */
    @Override
    public int statsEvaluationScoresOfTeacher() {
        return evalutionActivityMapper.statsEvaluationScoresOfTeacher();
    }

    /**
     * @author hpf
     * 检查本次评教活动是否统计完成
     * @return
     */
    @Override
    public int statNumberOfNoEvaluatedTeacher(){ return  evalutionActivityMapper.statNumberOfNoEvaluatedTeacher(); }


    public void statsTask(){
        int i = updateEvalutionActivityStatusTo3();
        if (i < 1){
            return;
        }
        statsEvaluationScoresOfTeacher();
    }



    /**
     * 批量删除评教活动管理
     *
     * @param evalutionActivityIds 需要删除的评教活动管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteEvalutionActivityByEvalutionActivityIds(Long[] evalutionActivityIds)
    {
        return evalutionActivityMapper.deleteEvalutionActivityByEvalutionActivityIds(evalutionActivityIds);
    }

    /**
     * 删除评教活动管理信息
     *
     * @param evalutionActivityId 评教活动管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvalutionActivityByEvalutionActivityId(Long evalutionActivityId)
    {
        return evalutionActivityMapper.deleteEvalutionActivityByEvalutionActivityId(evalutionActivityId);
    }
//--------------------------------------------------------------------------------------------------------------
    /**
     * 新增评教活动管理
     * @author hpf
     * @param evalutionActivity 评教活动管理
     * @return 结果
     * 11.11 修改了添加活动的同时插入evaluation中数据
     */
    @Transactional
    @Override
    public int insertEvalutionActivity(EvalutionActivity evalutionActivity)
    {
        evalutionActivity.setCreateTime(DateUtils.getNowDate());
//        1. 添加活动
        int r = evalutionActivityMapper.insertEvalutionActivity(evalutionActivity);
//        2. 添加evaluation
        evaluationService.add(evalutionActivity.getEvalutionActivityId(),evalutionActivity.getEvaluationPaperId()
                ,evalutionActivity.getCourseType(),evalutionActivity.getTerm());
        return r;
    }
    /**
     * 开启活动
     * @author hpf
     *
     */
    @Transactional
    @Override
    public int startEvaluationActivity(long activityId){
        EvalutionActivity evalutionActivity = selectEvalutionActivityByEvalutionActivityId(activityId);
        int result = evalutionActivityMapper.countHasSameCourseTypeNoOver(evalutionActivity.getCourseType());
        if(result>=1){
            return -1;     //存在没有结束的同类型活动，无法开启
        }else{
            evaluationService.deleteCourseclassHistory(activityId);
            evalutionActivity.setStatus("2");//更改状态为2，等待评价（开启）
           return  updateEvalutionActivity(evalutionActivity);
        }
    }

    @Override
    public List<String> selectTermByStudentIdAndStatus(Long studentId, String status) {
        Map<String,Object>map=new HashMap<>();
        map.put("studentId",studentId);
        map.put("status",status);
        return evalutionActivityMapper.selectTermByStudentIdAndStatus(map);
    }

    @Override
    public List<EvaluationPaper> selectActivityPaperByTerm(Long studentId,String term) {
        Map<String,Object>map=new HashMap<>();
        map.put("studentId",studentId);
        map.put("term",term);
        List<EvalutionActivity> evalutionActivities = evalutionActivityMapper.selectActivityPaperByTerm(map);
        List<EvaluationPaper>papers=new ArrayList<>();
        for (EvalutionActivity evalutionActivity:evalutionActivities
             ) {
            EvaluationPaper evaluationPaper = evaluationPaperMapper.selectEvaluationPaperByEvaluationPaperId(evalutionActivity.getEvaluationPaperId());
            evaluationPaper.setEvalutionActivity(evalutionActivity);
            papers.add(evaluationPaper);
        }
        return papers;
    }
}
